package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.mapper.MemberMapper;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

@Service(interfaceClass = MemberService.class)
public class MemberServiceImpl implements MemberService {
    @Autowired
    private MemberMapper memberMapper;
    @Override
    public Member findByTelephone(String telephone) {

        return memberMapper.findByTelephone(telephone);
    }

    @Override
    public void add(Member member) {
        memberMapper.add(member);
    }

    @Override
    public Map getMemberReport() throws Exception {

        //获取一个日历对象
        Calendar calendar = Calendar.getInstance();
        //将日期向前推12个月
        calendar.add(Calendar.MONTH,-12);
        //定义一个集合,用来存储每个月的会员数
        ArrayList<Integer> memberCounts = new ArrayList<>();
        //定义一个集合,用来存储过去十二个月
        ArrayList<String> memberMouths = new ArrayList<>();
        for (int i = 0; i <12 ; i++) {
            Date time = calendar.getTime();
            String date = DateUtils.parseDate2String(time,"yyyy-MM");
            memberMouths.add(date);

            //定义每个月的查询的起始值
            String dateBegin = date+"-01";

            //定义每个月的查询的结束值
            String dateEnd = date+"-31";
            //查询每个月的会员数量
            int count = memberMapper.findMemberCountByMonth(dateBegin,dateEnd);
            memberCounts.add(count);

            System.out.println(date);
            calendar.add(Calendar.MONTH,1);

        }
       Map map = new HashMap<>();
        map.put("memberMonths",memberMouths);
        map.put("memberCounts",memberCounts);

        return map;
    }
}
